package org.liquidByte.trafficMustang.androidApp.listview.adapter;

import org.liquidByte.trafficMustang.R;
import org.liquidByte.trafficMustang.androidApp.listview.row.ListViewRow;
import org.liquidByte.trafficMustang.androidApp.listview.row.RowActionListener;
import org.liquidByte.trafficMustang.androidApp.listview.row.TrechoRow;
import org.liquidByte.trafficMustang.androidApp.services.dto.TrechoDTO;

import android.content.Context;
import android.view.View;

public class TrechoRowAdapter extends RowAdapter<TrechoDTO> {
	
	private RowActionListener<TrechoRow> embarcarActionListener;
	private RowActionListener<TrechoRow> desembarcarActionListener;
	private RowActionListener<TrechoRow> cancelarActionListener;
	private RowActionListener<TrechoRow> localizacaoActionListener;
	
	public TrechoRowAdapter(Context ctx) {
		super(ctx);
	}
	
	@Override
	protected int getViewResource() {
		return R.layout.listview_row_trecho;
	}

	@Override
	public long getItemId(int position) {
		TrechoDTO trecho = (TrechoDTO)this.getItem(position);
		if (trecho == null) {
			return 0;
		} else {
			return trecho.getId();
		}
	}
	
	public void setEmbarcarActionListener(
			RowActionListener<TrechoRow> embarcarActionListener) {
		this.embarcarActionListener = embarcarActionListener;
	}

	public void setDesembarcarActionListener(
			RowActionListener<TrechoRow> desembarcarActionListener) {
		this.desembarcarActionListener = desembarcarActionListener;
	}

	public void setCancelarActionListener(
			RowActionListener<TrechoRow> cancelarActionListener) {
		this.cancelarActionListener = cancelarActionListener;
	}
	
	public void setLocalizacaoActionListener(
			RowActionListener<TrechoRow> localizacaoActionListener) {
		this.localizacaoActionListener = localizacaoActionListener;
	}

	@Override
	protected ListViewRow createRow(View view) {
		TrechoRow row = new TrechoRow(this, view);
		row.setCancelarActionListener(new RowActionListener<TrechoRow>() {
			@Override
			public void actionPerformed(TrechoRow row) {
				TrechoRowAdapter adapter = TrechoRowAdapter.this;
				if (adapter.cancelarActionListener != null) {
					adapter.cancelarActionListener.actionPerformed(row);
				}
			}
		});
		row.setDesembarcarActionListener(new RowActionListener<TrechoRow>() {
			
			@Override
			public void actionPerformed(TrechoRow row) {
				TrechoRowAdapter adapter = TrechoRowAdapter.this;
				if (adapter.desembarcarActionListener != null) {
					adapter.desembarcarActionListener.actionPerformed(row);
				}
			}
		});
		row.setEmbarcarActionListener(new RowActionListener<TrechoRow>() {
			
			@Override
			public void actionPerformed(TrechoRow row) {
				TrechoRowAdapter adapter = TrechoRowAdapter.this;
				if (adapter.embarcarActionListener != null) {
					adapter.embarcarActionListener.actionPerformed(row);
				}				
			}
		});
		row.setLocalizacaoActionListener(new RowActionListener<TrechoRow>() {
			@Override
			public void actionPerformed(TrechoRow row) {
				TrechoRowAdapter adapter = TrechoRowAdapter.this;
				if (adapter.localizacaoActionListener != null) {
					adapter.localizacaoActionListener.actionPerformed(row);
				}
			}
		});
		return row;
	}
}
